/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Reportes.java
 *
 * Created on 18/11/2011, 08:58:54 PM
 */
package Vista;
import DAO_Modelo.dto.*;
import DAO_Modelo.exceptions.CuentaDaoException;
import DAO_Modelo.exceptions.NotificacionesjudicialesDaoException;
import DAO_Modelo.exceptions.PrestamoDaoException;
import DAO_Modelo.exceptions.SocioDaoException;
import com.toedter.calendar.JCalendar;
import java.io.File;
import java.io.IOException;
import java.sql.SQLData;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.Date;
import javax.swing.JOptionPane;
import jxl.*;
import jxl.read.biff.BiffException;
import jxl.write.*;
/**
 *
 * @author jose,digberht
 * 
 * 
 */

public class Reportes extends javax.swing.JFrame {

    /** Creacion del nuevo formulario de  Reportes */
    static Vector<Notificacionesjudiciales> demand=new Vector<Notificacionesjudiciales>();
    public static String addresexcel="C:/Archivos de programa/Microsoft Office/Office12/EXCEL.exe";
    public static String nameexcel="C:/Demandas/Relacion_Socios_Demanda_"+NombreArchivo()+"a.xls";
    public Reportes() {
        initComponents();
    }

    public static String NombreArchivo(){
        Calendar calendarioAhora = Calendar.getInstance();
        calendarioAhora.add(Calendar.HOUR, -5);
        Date dateAhora = calendarioAhora.getTime();
        SimpleDateFormat formato = new SimpleDateFormat("yyyy-MM-ddHH-mm-ss");
        
        return (formato.format(dateAhora));
    }
   
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jButton_GenerarReporte = new javax.swing.JButton();
        jComboBox1 = new javax.swing.JComboBox();
        jComboBox2 = new javax.swing.JComboBox();
        jDateChooser1 = new com.toedter.calendar.JDateChooser();
        jDateChooser2 = new com.toedter.calendar.JDateChooser();
        jButtonReportes_Cerrar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Reportes"));

        jLabel1.setText("Juzgado");

        jLabel2.setText("Estado de Proceso Judicial");

        jLabel3.setText("Fecha");

        jLabel4.setText("a");

        jButton_GenerarReporte.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Iconos/excel.JPG"))); // NOI18N
        jButton_GenerarReporte.setText("Generar  Reporte");
        jButton_GenerarReporte.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_GenerarReporteActionPerformed(evt);
            }
        });

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(Sigele.Distritos));

        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(Sigele.EstadoProceso));

        jButtonReportes_Cerrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Iconos/salisr.JPG"))); // NOI18N
        jButtonReportes_Cerrar.setText("Cerrar");
        jButtonReportes_Cerrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonReportes_CerrarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jButton_GenerarReporte, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel1)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(jLabel3)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(jDateChooser1, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jLabel2))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(28, 28, 28)
                        .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 11, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(21, 21, 21)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jComboBox2, 0, 114, Short.MAX_VALUE)
                            .addComponent(jComboBox1, javax.swing.GroupLayout.Alignment.LEADING, 0, 114, Short.MAX_VALUE)
                            .addComponent(jDateChooser2, javax.swing.GroupLayout.DEFAULT_SIZE, 114, Short.MAX_VALUE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonReportes_Cerrar, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(26, 26, 26))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(17, 17, 17)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(17, 17, 17)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2)))
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jDateChooser2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 53, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButtonReportes_Cerrar)
                            .addComponent(jButton_GenerarReporte)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addComponent(jLabel3))
                    .addComponent(jDateChooser1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
/** 
	 * Funcion de evento del boton de Generar Reporte que muestra en un excell los resultados
	 */
private void jButton_GenerarReporteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_GenerarReporteActionPerformed
// TODO add your handling code here:
    String juzgado,estado;
    juzgado = jComboBox1.getSelectedItem().toString(); 
    estado = jComboBox2.getSelectedItem().toString();
    java.util.Date fechaIni=jDateChooser1.getDate();
    java.util.Date fechaFin=jDateChooser2.getDate();
    
    Notificacionesjudiciales notificacionesjudiciales[];
    Vector<Socio> socios=new Vector<Socio>();
    Vector<Cuenta> cuentas=new Vector<Cuenta>();
    Vector<Prestamo> prestamos=new Vector<Prestamo>();
    Vector<Notificacionesjudiciales> nnj=new Vector<Notificacionesjudiciales>();
    if(!juzgado.equals("") && estado.equals("") && fechaIni==null && fechaFin==null){
            try {
                
                notificacionesjudiciales=Sigele.not_judDao.findWhereJuzgadoEquals(juzgado);
                if(notificacionesjudiciales.length>0){
                    for (int i = 0; i < notificacionesjudiciales.length; i++) {
                        socios.add(Sigele.socioDao.findByPrimaryKey(notificacionesjudiciales[i].getCodigoSocio()));
                    }
                    for (int i = 0; i < socios.size(); i++) {
                        System.out.println(socios.get(i).getCodigoSocio());
                        Cuenta cuentas_socio[] = (Sigele.cuentaDao.findWhereCodigoSocioEquals(socios.get(i).getCodigoSocio()));
                        cuentas.add(cuentas_socio[0]);
                    }
                    for (int i = 0; i < socios.size(); i++) {
                        Prestamo prestamo[] = (Sigele.prestamoDao.findWhereCodigoSocioEquals(socios.get(i).getCodigoSocio()));
                        prestamos.add(prestamo[0]);
                    }
                    System.out.println(notificacionesjudiciales.length);
                    System.out.println(socios.size());
                    System.out.println(cuentas.size());
                    escribirExcel(notificacionesjudiciales, socios, cuentas, prestamos);
                    
                }else{
                    JOptionPane.showMessageDialog(null, "No se encontro Socios Por Juzgado "+juzgado);
                }
                
            } catch (PrestamoDaoException ex) {
                Logger.getLogger(Reportes.class.getName()).log(Level.SEVERE, null, ex);
            } catch (NotificacionesjudicialesDaoException ex) {
                Logger.getLogger(Reportes.class.getName()).log(Level.SEVERE, null, ex);
            }
            catch (SocioDaoException ex) {
                        Logger.getLogger(Reportes.class.getName()).log(Level.SEVERE, null, ex);
                    }
            catch (CuentaDaoException ex) {
                        Logger.getLogger(Reportes.class.getName()).log(Level.SEVERE, null, ex);
                    }
             
    }
    if(juzgado.equals("") && !estado.equals("") && fechaIni==null && fechaFin==null){
            try {
                notificacionesjudiciales=Sigele.not_judDao.findWhereEstadoProcesoJudicialEquals(estado);
                for (int i = 0; i < notificacionesjudiciales.length; i++) {
                   
                        socios.add(Sigele.socioDao.findByPrimaryKey(notificacionesjudiciales[i].getCodigoSocio()));
                    }
                            System.out.println(notificacionesjudiciales.length);
                            System.out.println(socios.size());
                            escribirExcel(notificacionesjudiciales,socios);
                            //Abrir();
            } catch (NotificacionesjudicialesDaoException ex) {
                Logger.getLogger(Reportes.class.getName()).log(Level.SEVERE, null, ex);
            }catch (SocioDaoException ex) {
                        Logger.getLogger(Reportes.class.getName()).log(Level.SEVERE, null, ex);
                    }
    }
    if(juzgado.equals("") && estado.equals("") && fechaIni!=null && fechaFin!=null){
            try {
                notificacionesjudiciales=Sigele.not_judDao.findAll();
                for (int i = 0; i < notificacionesjudiciales.length; i++) {
                    if(notificacionesjudiciales[i].getFechaLlegada().after(fechaIni) && notificacionesjudiciales[i].getFechaLlegada().before(fechaFin)){
                        nnj.add(notificacionesjudiciales[i]);
                        socios.add(Sigele.socioDao.findByPrimaryKey(notificacionesjudiciales[i].getCodigoSocio()));
                    }
                }
                for (int i = 0; i < socios.size(); i++) {
                        System.out.println(socios.get(i).getCodigoSocio());
                        Cuenta cuentas_socio[] = (Sigele.cuentaDao.findWhereCodigoSocioEquals(socios.get(i).getCodigoSocio()));
                        cuentas.add(cuentas_socio[0]);
                    }
                    for (int i = 0; i < socios.size(); i++) {
                        Prestamo prestamo[] = (Sigele.prestamoDao.findWhereCodigoSocioEquals(socios.get(i).getCodigoSocio()));
                        prestamos.add(prestamo[0]);
                    }
                System.out.println(nnj.size());
                System.out.println(socios.size());
                System.out.println(cuentas.size());
                System.out.println(prestamos.size());
                escribirExcel_nj(nnj,socios,cuentas,prestamos,fechaIni,fechaFin);
            } catch (PrestamoDaoException ex) {
                Logger.getLogger(Reportes.class.getName()).log(Level.SEVERE, null, ex);
            } catch (CuentaDaoException ex) {
                Logger.getLogger(Reportes.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SocioDaoException ex) {
                Logger.getLogger(Reportes.class.getName()).log(Level.SEVERE, null, ex);
            } catch (NotificacionesjudicialesDaoException ex) {
                Logger.getLogger(Reportes.class.getName()).log(Level.SEVERE, null, ex);
            }
    }
}//GEN-LAST:event_jButton_GenerarReporteActionPerformed
/** 
	 * Funcion de evento del boton de cerrar el cual cierra el formulario actual activo
	 */
private void jButtonReportes_CerrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonReportes_CerrarActionPerformed
// TODO add your handling code here:
    this.dispose();
}//GEN-LAST:event_jButtonReportes_CerrarActionPerformed
   /** 
	 * Funcion que despliega el archivo ejecutable de excell
	 */  
   public static void Abrir(String name){
    
    Runtime excel = Runtime.getRuntime();
        try{
        excel.exec("C:/Program Files/Microsoft Office/Office12/EXCEL.exe"+" "+name);
        }
        catch(Exception e){
        System.out.println(e);
        }
    }
   public static void escribirExcel_nj(Vector<Notificacionesjudiciales> nj,Vector<Socio> socios,Vector<Cuenta> cuentas,Vector<Prestamo> prestamos,java.util.Date fechaI,java.util.Date fechaf)
    {
        java.util.Date fecha = new Date();
        String nombre="C:/Demandas/Relacion_Socios_Demanda_Fecha.xls";
        
        try
        {
            //Se crea el libro Excel
            WritableWorkbook workbook =Workbook.createWorkbook(new File(nombre));
 
            //Se crea una nueva hoja dentro del libro
            WritableSheet sheet =workbook.createSheet("Notificaciones Por Fecha", 0);
 
            //Creamos celdas de varios tipos
            WritableFont times16font = new WritableFont(WritableFont.TIMES, 10, WritableFont.BOLD, true); 
            times16font.setColour(Colour.BLACK);
            WritableCellFormat formato1 = new WritableCellFormat(times16font); 
            formato1.setBackground(Colour.YELLOW);
            
            sheet.addCell(new jxl.write.Label(1, 2, "Relacion de socios con demanda del "+new java.sql.Date(fechaI.getTime()).toString()+" al "+new java.sql.Date(fechaf.getTime()).toString(),formato1));
            sheet.mergeCells(1, 2, 4, 2);
            sheet.addCell(new jxl.write.Label(1, 3, "Nº",formato1));
            sheet.setColumnView(1,4);
            sheet.addCell(new jxl.write.Label(2, 3, "Cuenta",formato1));
            sheet.setColumnView(2,8);
            sheet.addCell(new jxl.write.Label(3, 3, "Socio",formato1));
            sheet.setColumnView(3,30);
            sheet.addCell(new jxl.write.Label(4, 3, "Fecha de Prestamo",formato1));
            sheet.setColumnView(4,18);
            sheet.addCell(new jxl.write.Label(5, 3, "Monto Original",formato1));
            sheet.setColumnView(5,14);
            sheet.addCell(new jxl.write.Label(6, 3, "Monto Demandado",formato1));
            sheet.setColumnView(6,16);
            sheet.addCell(new jxl.write.Label(7, 3, "Juzgado",formato1));
            sheet.setColumnView(7,25);
            sheet.addCell(new jxl.write.Label(8, 3, "Ultima Diligencia",formato1));
            sheet.setColumnView(8,18);
            sheet.addCell(new jxl.write.Label(9, 3, "Est. Proceso Jud.",formato1));
            sheet.setColumnView(9,18);
            sheet.addCell(new jxl.write.Label(10, 3, "Fecha Ult. Pago",formato1));
            sheet.setColumnView(10,15);
            sheet.addCell(new jxl.write.Label(11, 3, "Saldo Cap. Actual",formato1));
            sheet.setColumnView(11,18);
            sheet.addCell(new jxl.write.Label(12, 3, "Monto Recuperado",formato1));
            sheet.setColumnView(12,18);
            sheet.addCell(new jxl.write.Label(13, 3, "Observaciones",formato1));
            sheet.setColumnView(13,40);
            sheet.addCell(new jxl.write.Label(14, 3, "Fecha Llegada",formato1));
            sheet.setColumnView(14,15);
            
            for (int j = 0; j < nj.size(); j++) {
            //sheet.addCell(new jxl.write.Label(0, 0, "Nº",formato1));
            sheet.addCell(new jxl.write.Label(1, 4+j, String.valueOf(1+j)));
            sheet.addCell(new jxl.write.Label(2, 4+j, String.valueOf(cuentas.get(j).getCodigoCuenta())));
            sheet.addCell(new jxl.write.Label(3, 4+j, socios.get(j).getNombre()+" "+socios.get(j).getApellidos()));
            sheet.addCell(new jxl.write.Label(4, 4+j, new java.sql.Date(prestamos.get(j).getFechaPrestamo().getTime()).toString()));
            sheet.addCell(new jxl.write.Label(5, 4+j, String.valueOf(prestamos.get(j).getMontoOriginal())));
            sheet.addCell(new jxl.write.Label(6, 4+j, String.valueOf(nj.get(j).getMontoDemandado())));
            sheet.addCell(new jxl.write.Label(7, 4+j, String.valueOf(nj.get(j).getJuzgado())));
            sheet.addCell(new jxl.write.Label(8, 4+j, new java.sql.Date(nj.get(j).getUltimaDiligencia().getTime()).toString()));
            sheet.addCell(new jxl.write.Label(9, 4+j, String.valueOf(nj.get(j).getEstadoProcesoJudicial())));
            sheet.addCell(new jxl.write.Label(10, 4+j, new java.sql.Date(nj.get(j).getFehaUltimoPago().getTime()).toString()));
            sheet.addCell(new jxl.write.Label(11, 4+j, String.valueOf(nj.get(j).getSaldoActual())));
            sheet.addCell(new jxl.write.Label(12, 4+j, String.valueOf(nj.get(j).getMontoRecuperado())));
            sheet.addCell(new jxl.write.Label(13, 4+j, String.valueOf(prestamos.get(j).getObservaciones())));
            sheet.addCell(new jxl.write.Label(14, 4+j, new java.sql.Date(nj.get(j).getFechaLlegada().getTime()).toString()));
            }
            
            workbook.write();
            workbook.close();
            Abrir(nombre);
 
            System.out.println("Ejemplo finalizado.");
        }
        catch (IOException ex)
        {
            System.out.println("Error al crear el fichero.");
        }
        catch (WriteException ex)
        {
            System.out.println("Error al escribir el fichero.");
        }
    }
   public static void escribirExcel(Notificacionesjudiciales nj[],Vector<Socio> socios)
    {
        java.util.Date fecha = new Date();
        String nombre="C:/Demandas/Relacion_Socios_Demanda.xls";
        
        try
        {
            //Se crea el libro Excel
            WritableWorkbook workbook =Workbook.createWorkbook(new File(nombre));
 
            //Se crea una nueva hoja dentro del libro
            WritableSheet sheet =workbook.createSheet("Por Notificaciones Judiciales", 0);
 
            //Creamos celdas de varios tipos
            WritableFont times16font = new WritableFont(WritableFont.TIMES, 10, WritableFont.BOLD, true); 
            times16font.setColour(Colour.BLACK);
            WritableCellFormat formato1 = new WritableCellFormat(times16font); 
            formato1.setBackground(Colour.YELLOW);
            
            sheet.addCell(new jxl.write.Label(1, 2, "Notificacion de "+nj[0].getEstadoProcesoJudicial(),formato1));
            sheet.mergeCells(1, 2, 3, 2);
            sheet.addCell(new jxl.write.Label(1, 3, "Nº",formato1));
            sheet.setColumnView(1,4);
            sheet.addCell(new jxl.write.Label(2, 3, "Fecha de LLegada",formato1));
            sheet.setColumnView(2,17);
            sheet.addCell(new jxl.write.Label(3, 3, "Demandado",formato1));
            sheet.setColumnView(3,30);
            sheet.addCell(new jxl.write.Label(4, 3, "Asunto",formato1));
            sheet.setColumnView(4,25);
            sheet.addCell(new jxl.write.Label(5, 3, "Juzgado",formato1));
            sheet.setColumnView(5,25);
            
            for (int j = 0; j < nj.length; j++) {
            //sheet.addCell(new jxl.write.Label(0, 0, "Nº",formato1));
            sheet.addCell(new jxl.write.Label(1, 4+j, String.valueOf(1+j)));
            sheet.addCell(new jxl.write.Label(2, 4+j, String.valueOf(nj[j].getFechaLlegada())));
            sheet.addCell(new jxl.write.Label(3, 4+j, socios.get(j).getNombre()+" "+socios.get(j).getApellidos()));
            sheet.addCell(new jxl.write.Label(4, 4+j, nj[j].getAsunto()));
            sheet.addCell(new jxl.write.Label(5, 4+j, nj[j].getJuzgado()));
           
            }
            
            workbook.write();
            workbook.close();
            
            System.out.println("Ejemplo finalizado.");
            Abrir(nombre);
        }
        catch (IOException ex)
        {
            System.out.println("Error al crear el fichero.");
        }
        catch (WriteException ex)
        {
            System.out.println("Error al escribir el fichero.");
        }
    }
   /** 
	 * Funcion que escribe el archivo ejecutable de excell
	 */   
    public static void escribirExcel(Notificacionesjudiciales nj[],Vector<Socio> socios,Vector<Cuenta> cuentas,Vector<Prestamo> prestamos)
    {
        java.util.Date fecha = new Date();
        String nombre="C:/Demandas/Relacion_Socios_Demanda_Juzgado.xls";
        
        try
        {
            //Se crea el libro Excel
            WritableWorkbook workbook =Workbook.createWorkbook(new File(nombre));
 
            //Se crea una nueva hoja dentro del libro
            WritableSheet sheet =workbook.createSheet("Por Juzgado", 0);
 
            //Creamos celdas de varios tipos
            WritableFont times16font = new WritableFont(WritableFont.TIMES, 10, WritableFont.BOLD, true); 
            times16font.setColour(Colour.BLACK);
            WritableCellFormat formato1 = new WritableCellFormat(times16font); 
            formato1.setBackground(Colour.YELLOW);
            
            sheet.addCell(new jxl.write.Label(1, 2, "Juzgado de "+nj[0].getJuzgado(),formato1));
            sheet.mergeCells(1, 2, 3, 2);
            sheet.addCell(new jxl.write.Label(1, 3, "Nº",formato1));
            sheet.setColumnView(1,4);
            sheet.addCell(new jxl.write.Label(2, 3, "Cta.",formato1));
            sheet.setColumnView(2,10);
            sheet.addCell(new jxl.write.Label(3, 3, "Socio",formato1));
            sheet.setColumnView(3,30);
            sheet.addCell(new jxl.write.Label(4, 3, "Fecha de Prestamo",formato1));
            sheet.setColumnView(4,18);
            sheet.addCell(new jxl.write.Label(5, 3, "Monto  Original",formato1));
            sheet.setColumnView(5,14);
            sheet.addCell(new jxl.write.Label(6, 3, "Monto  Demandado",formato1));
            sheet.setColumnView(6,16);
            
            for (int j = 0; j < nj.length; j++) {
            //sheet.addCell(new jxl.write.Label(0, 0, "Nº",formato1));
            sheet.addCell(new jxl.write.Label(1, 4+j, String.valueOf(1+j)));
            sheet.addCell(new jxl.write.Label(2, 4+j, String.valueOf(cuentas.get(j).getCodigoCuenta())));
            sheet.addCell(new jxl.write.Label(3, 4+j, socios.get(j).getNombre()+" "+socios.get(j).getApellidos()));
            sheet.addCell(new jxl.write.Label(4, 4+j, new java.sql.Date(prestamos.get(j).getFechaPrestamo().getTime()).toString()));
            sheet.addCell(new jxl.write.Label(5, 4+j, String.valueOf(nj[j].getMontoOriginal())));
            sheet.addCell(new jxl.write.Label(6, 4+j, String.valueOf(nj[j].getMontoDemandado())));
            
                
            }
            
            workbook.write();
            workbook.close();
            Abrir(nombre);
 
            System.out.println("Ejemplo finalizado.");
        }
        catch (IOException ex)
        {
            System.out.println("Error al crear el fichero.");
        }
        catch (WriteException ex)
        {
            System.out.println("Error al escribir el fichero.");
        }
    }
 /** 
	 * Funcion que lee el archivo generado de excell
	 */  
    public static void leerExcel()
    {
        try
        {
            //Se abre el fichero Excel
            Workbook workbook = Workbook.getWorkbook(new File("ejemplo.xls"));
 
            //Se obtiene la primera hoja
            Sheet sheet = workbook.getSheet(0);
 
            //Se leen las primeras 5 celdas
            for(int i=0; i<5; i++)
            {
                //Se obtiene la celda i-esima
                Cell cell = sheet.getCell(i,0);
 
                //Se imprime en pantalla la celda según su tipo
                if (cell.getType() == CellType.NUMBER)
                {
                    System.out.println("Número: " + ((NumberCell)cell).getValue());
                }
                else if (cell.getType() == CellType.LABEL)
                {
                    System.out.println("String: " + ((LabelCell)cell).getString());
                }
                else if (cell.getType() == CellType.BOOLEAN)
                {
                    System.out.println("Boolean: " + ((BooleanCell)cell).getValue());
                }
                else if (cell.getType() == CellType.DATE)
                {
                    System.out.println("Fecha: " + ((DateCell)cell).getDate());
                }
            }
        }
        catch (Exception ex)
        {
            System.out.println("Error!");
        }
    }
     
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Reportes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Reportes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Reportes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Reportes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        
        
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Reportes().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonReportes_Cerrar;
    private javax.swing.JButton jButton_GenerarReporte;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private com.toedter.calendar.JDateChooser jDateChooser1;
    private com.toedter.calendar.JDateChooser jDateChooser2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables
}
